﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using EQT.Model;

public partial class MasterPage : System.Web.UI.MasterPage
{
    UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        if (context == null)
        {
            Response.Redirect("~/login.aspx?r");
        }
        else//有效登录
        {
            int nowGroup = this.GetGroupIndex(this.Request.Url.AbsolutePath);
            if (nowGroup == -1)
            {
                nowGroup = int.Parse(Session["PreNowGroup"] == null ? "0" : Session["PreNowGroup"].ToString());
                Session["PreEffectiveURL"] = this.Request.Url.AbsolutePath;
                Session["PreNowGroup"] = nowGroup;
            }
            else
            {
                //前一个有效URL
                Session["PreEffectiveURL"] = this.Request.Url.AbsolutePath;
                //前一个组
                Session["PreNowGroup"] = nowGroup;
            }

            //根据用户的角色确定了菜单
            this.SetMainMenu(nowGroup);
            this.SetSubMenu(nowGroup, Session["PreEffectiveURL"].ToString());
        }
      
    }
    /// <summary>
    /// 设置第二集菜单,根据用户的Context获得的
    /// </summary>
    /// <param name="nowGroup"></param>
    /// <param name="pageName"></param>
    private void SetSubMenu(int nowGroup, string pageName)
    {
        if (context == null) return;
        string allLi = "";

        Manifest mf = Utility.GetUserManifestModel(context);
        if (mf == null ||mf.GroupList.Count==0)
        {
            Utility.GlobalLog.ErrorFormat("用户上下文User_ID={0}无效，没有得到菜单", context.UserID);
            Utility.SiteError("登录无效！");
            return;
        }


        for (int i = 0; i < mf.GroupList[nowGroup].SubMenuList.Count; i++)
        {
            if (mf.GroupList[nowGroup].SubMenuList[i].Url.ToLower().Equals(pageName.ToLower()))
            {
                allLi = allLi + "<li class='subselected'><a href='" + mf.GroupList[nowGroup].SubMenuList[i].Url + "' style='color:black;'>" + mf.GroupList[nowGroup].SubMenuList[i].DisPlayText + "</a></li>";
            }
            else
            {
                allLi = allLi + "<li><a href='" + mf.GroupList[nowGroup].SubMenuList[i].Url + "'>" + mf.GroupList[nowGroup].SubMenuList[i].DisPlayText + "</a></li>";
            }
        }

        string htmlTopCode = "<ul>" + allLi + "</ul>";
        TableCell tcl = new TableCell();
        TableRow trw = new TableRow();
        tcl.Text = htmlTopCode;
        trw.Cells.Add(tcl);
        this.tblSubMenu.Rows.Add(trw);
    }

    /// <summary>
    /// 设置第一级菜单，根据用户的Context获得的
    /// </summary>
    /// <param name="selectedIndex"></param>
    protected void SetMainMenu(int selectedIndex)
    {
        if (context == null) return;
        string allLi = "";
        Manifest mf = Utility.GetUserManifestModel(context);
        if (mf == null)
        {
            Utility.GlobalLog.ErrorFormat("用户上下文User_ID={0}无效，没有得到菜单", context.UserID);
            Utility.SiteError("登录无效！");
            return;
        }

        for (int i = 0; i < mf.GroupList.Count; i++)
        {
            if (i == selectedIndex)
            {
                allLi = allLi + "<li id='one" + i.ToString() + "'><a id='N" + i.ToString() + "' class='selected' href='" + mf.GroupList[i].Url + "'>" + mf.GroupList[i].DisplayText + "</a></li>";
            }
            else
            {
                allLi = allLi + "<li id='one" + i.ToString() + "'><a id='N" + i.ToString() + "' href='" + mf.GroupList[i].Url + "'>" + mf.GroupList[i].DisplayText + "</a></li>";
            }
        }

        string htmlTopCode = "<div  id ='menu'><ul>" + allLi + "</ul> <div id='menuright'>" + ServerValidate.GetChineseDate() + "  " + ServerValidate.GetWeekDay() + " 当前用户：" + context.UserName + "<a  href='login.aspx?r'> 注销</a></div></div>";

        TableCell tcl = new TableCell();
        TableRow trw = new TableRow();
        tcl.Text = htmlTopCode;
        trw.Cells.Add(tcl);
        this.tblMenu.Rows.Add(trw);
    }

    protected int GetGroupIndex(string pageName)
    {
        Manifest mf = Utility.GetUserManifestModel(context);
        if (mf == null)
        {
            Utility.GlobalLog.ErrorFormat("用户上下文User_ID={0}无效，没有得到菜单", context.UserID);
            Utility.SiteError("登录无效！");
            return 0;
        }
        pageName = pageName.ToLower();

        for (int i = 0; i < mf.GroupList.Count; i++)
        {
            if (mf.GroupList[i].Url.ToLower().Equals(pageName))
            {
                return i;
            }
            else
            {
                for (int j = 0; j < mf.GroupList[i].SubMenuList.Count; j++)
                {
                    if (mf.GroupList[i].SubMenuList[j].Url.ToLower().Equals(pageName))
                    {
                        return i;
                    }
                }
            }
        }
        return -1;
    }
}
